#!/bin/bash
# parameter 1: url, only GET method supported now
# parameter 2: text needs to be contained
# parameter 3: retry count, if check failed we will wait 1s before next retry, until retry time exceeds retry count

set -eu

url=$1
contain=$2
retry_count=$3

shift 3

counter=0
while [ $counter -lt $retry_count ]; do
    got=$(curl -s $url | grep "$contain" | wc -l)
    if [[ $got -gt 0 ]]; then
        echo "HTTP $url is alive"
        exit 0
    fi
    ((counter+=1))
    echo "wait for HTTP alive for $counter-th time"
    sleep 1
done

echo "HTTP $url is not alive"
exit 1
